using Npgsql;
using System;
using System.Threading.Tasks;

namespace CheckOperationLogs
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // 数据库连接字符串
            string connectionString = "Host=localhost;Database=GroupSixDb;Username=postgres;Password=123456";

            try
            {
                using (var connection = new NpgsqlConnection(connectionString))
                {
                    await connection.OpenAsync();
                    Console.WriteLine("数据库连接成功");

                    // 检查操作日志表是否存在
                    using (var command = new NpgsqlCommand(
                        "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'gs_operation_logs')", 
                        connection))
                    {
                        var tableExists = await command.ExecuteScalarAsync();
                        Console.WriteLine($"操作日志表是否存在: {tableExists}");
                    }

                    // 检查操作日志表结构
                    using (var command = new NpgsqlCommand(
                        "SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'gs_operation_logs' ORDER BY ordinal_position", 
                        connection))
                    {
                        using (var reader = await command.ExecuteReaderAsync())
                        {
                            Console.WriteLine("\n操作日志表结构:");
                            while (await reader.ReadAsync())
                            {
                                Console.WriteLine($"  {reader["column_name"]}: {reader["data_type"]}");
                            }
                        }
                    }

                    // 检查操作日志数据总数
                    using (var command = new NpgsqlCommand(
                        "SELECT COUNT(*) FROM gs_operation_logs", 
                        connection))
                    {
                        var count = await command.ExecuteScalarAsync();
                        Console.WriteLine($"\n操作日志总数: {count}");
                    }

                    // 检查最近的操作日志
                    using (var command = new NpgsqlCommand(
                        "SELECT \"Id\", \"OperationType\", \"Module\", \"OperatorName\", \"Result\", \"OperationTime\" FROM gs_operation_logs ORDER BY \"OperationTime\" DESC LIMIT 10", 
                        connection))
                    {
                        using (var reader = await command.ExecuteReaderAsync())
                        {
                            Console.WriteLine("\n最近10条操作日志:");
                            while (await reader.ReadAsync())
                            {
                                Console.WriteLine($"  ID: {reader["Id"]}");
                                Console.WriteLine($"  操作类型: {reader["OperationType"]}");
                                Console.WriteLine($"  模块: {reader["Module"]}");
                                Console.WriteLine($"  操作人: {reader["OperatorName"]}");
                                Console.WriteLine($"  结果: {reader["Result"]}");
                                Console.WriteLine($"  操作时间: {reader["OperationTime"]}");
                                Console.WriteLine("  ---");
                            }
                        }
                    }

                    // 检查文章相关的操作日志
                    using (var command = new NpgsqlCommand(
                        "SELECT COUNT(*) FROM gs_operation_logs WHERE \"Module\" = 'article'", 
                        connection))
                    {
                        var articleLogCount = await command.ExecuteScalarAsync();
                        Console.WriteLine($"\n文章相关操作日志数量: {articleLogCount}");
                    }

                    // 检查不同模块的操作日志分布
                    using (var command = new NpgsqlCommand(
                        "SELECT \"Module\", COUNT(*) as count FROM gs_operation_logs GROUP BY \"Module\" ORDER BY count DESC", 
                        connection))
                    {
                        using (var reader = await command.ExecuteReaderAsync())
                        {
                            Console.WriteLine("\n各模块操作日志分布:");
                            while (await reader.ReadAsync())
                            {
                                Console.WriteLine($"  {reader["Module"]}: {reader["count"]} 条");
                            }
                        }
                    }

                    // 检查不同操作类型的分布
                    using (var command = new NpgsqlCommand(
                        "SELECT \"OperationType\", COUNT(*) as count FROM gs_operation_logs GROUP BY \"OperationType\" ORDER BY count DESC", 
                        connection))
                    {
                        using (var reader = await command.ExecuteReaderAsync())
                        {
                            Console.WriteLine("\n各操作类型分布:");
                            while (await reader.ReadAsync())
                            {
                                Console.WriteLine($"  {reader["OperationType"]}: {reader["count"]} 条");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"错误: {ex.Message}");
                Console.WriteLine($"详细错误: {ex}");
            }

            Console.WriteLine("\n按任意键退出...");
            Console.ReadKey();
        }
    }
} 